package com.yakaten.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.seasar.framework.util.ResourceUtil;

import com.yakaten.dto.AmazonItemInfoDto;
import com.yakaten.dto.AmazonPriceTaskDto;
import com.yakaten.exception.AmazonException;
import com.yakaten.exception.SystemException;
import com.yakaten.logic.AmazonPriceLogic;

public class AmazonPriceRunner extends Thread {
	static Logger logger = Logger.getLogger(AmazonPriceRunner.class);

	private static final SimpleDateFormat sf = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");

	String SHOP_NAME = ResourceUtil.getProperties("yakaten.properties")
			.getProperty("system.shopName");

	public static int KENKAKU = 10;

	public void start() {
		super.start();
	}

	public List<AmazonPriceTaskDto> getWorkList() throws SystemException,
			AmazonException {
		List<AmazonPriceTaskDto> workList = new ArrayList<AmazonPriceTaskDto>();
		AmazonPriceLogic logic = new AmazonPriceLogic();

		List<AmazonPriceTaskDto> taskList = logic.getAllTaskList();
		Iterator<AmazonPriceTaskDto> taskListIt = taskList.iterator();
		while (taskListIt.hasNext()) {
			AmazonPriceTaskDto currentTask = taskListIt.next();
			if (currentTask.getStatus() == 1) {
				List<AmazonItemInfoDto> amazonItemInfoList = null;
				try{
					amazonItemInfoList = new AmazonItemService()
					.getItemInfo(currentTask.getAmazonItemId(), currentTask
							.getCondition());
				}catch(Exception e){
					logger.error(e);
				}

				if(amazonItemInfoList==null || amazonItemInfoList.size()==0){
//					currentTask.setStatus(0);
//					workList.add(currentTask);
				}else{
					AmazonItemInfoDto amazonItemInfo = amazonItemInfoList.get(0);
					currentTask.setAmazonItemPrice(amazonItemInfo.getItemPrice());
					if (SHOP_NAME.equals(amazonItemInfo.getShopName().trim())) {
						//
						if (amazonItemInfoList.size() > 1) {
							AmazonItemInfoDto amazonItemInfoSecond = amazonItemInfoList
									.get(1);
							currentTask.setWorkPrice(amazonItemInfoSecond
									.getItemPrice()+amazonItemInfoSecond.getShippingPrice() - 1);
							if (currentTask.getShopItemPrice() < currentTask
									.getWorkPrice()) {
								if(currentTask.getShopItemPrice()!=currentTask.getWorkPrice()){
									if (logger.isInfoEnabled()) {
										logger
												.info("update amazone price >>> shopItemId:"
														+ currentTask.getShopItemId()
														+ ":"
														+ currentTask.getShopItemName()+"  >  "+currentTask.getShopItemPrice()
														+ " >>> "
														+ currentTask.getWorkPrice());
									}
									currentTask.setShopItemPrice(currentTask.getWorkPrice());
									workList.add(currentTask);


								}

							}
						}
					} else {
						if (currentTask.getMinPrice() < amazonItemInfo
								.getItemPrice()+amazonItemInfo.getShippingPrice()) {
							currentTask
									.setWorkPrice(amazonItemInfo.getItemPrice()+amazonItemInfo.getShippingPrice() - 1);
							if (logger.isInfoEnabled()) {
								logger.info("update amazone price >>>>>> shopItemId:"
										+ currentTask.getShopItemId()
										+ ":"
										+ currentTask.getShopItemName()+"  >  "+currentTask.getShopItemPrice()
										+ " >>> "
										+ currentTask.getWorkPrice());
							}
							currentTask.setShopItemPrice(currentTask.getWorkPrice());
							workList.add(currentTask);
						}
					}
				}
				try {
					AmazonPriceRunner.sleep(800);
				} catch (InterruptedException e) {
				}
			}
		}

		return workList;
	}

	public List<AmazonPriceTaskDto> updateMyShop(
			List<AmazonPriceTaskDto> workList) {
		List<AmazonPriceTaskDto> resultList = new ArrayList<AmazonPriceTaskDto>();
		return resultList;
	}

	public static void main(String[] args) {
	}

	public void run() {
		while (true) {
			try{
				if("1".equals(ResourceUtil.getProperties("yakaten.properties").getProperty("amazon.price.runner.run"))){
					if (logger.isInfoEnabled()) {
						String message = "--------amazone task start on:"
								+ sf.format(new Date()) + "---------";
						logger.info(message);
					}

					List<AmazonPriceTaskDto> workList = getWorkList();
					if (workList.size() > 0) {
						new AmazonWebservice().updateItemPrice(workList);
						AmazonPriceLogic logic = new AmazonPriceLogic();
						logic.updateTask(workList);

					}
				}
			}catch(Exception e){
				logger.error(e);
			}finally{
				if (logger.isInfoEnabled()) {
					String message = "--------amazone task end on:"
							+ sf.format(new Date()) + "---------";
					logger.info(message);
				}
			}


			// this.sleep(10*60*1000);
			try {
				AmazonPriceRunner.sleep(KENKAKU * 60 * 1000);
			} catch (InterruptedException e) {
				logger.error(e);
			}
		}
	}
}
